package com.mutouren.web.org;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mutouren.common.entity.Result;
import com.mutouren.common.entity.ResultCode;
import com.mutouren.common.entity.ResultInfo;
import com.mutouren.common.entity.ValidState;
import com.mutouren.common.session.UserInfo;
import com.mutouren.common.utils.StringUtils;
import com.mutouren.modules.org.entity.IdentityType;
import com.mutouren.modules.org.model.Person;
import com.mutouren.modules.org.service.PersonService;
import com.mutouren.web.BaseApi;

@Controller
@RequestMapping(value="/api/org")
public class UserApi extends BaseApi {

	@Resource
	private PersonService personService;	

	@RequestMapping(value="/login.action")
	@ResponseBody
	public ResultInfo<UserInfo> login(String loginName, String password) {
		ResultInfo<UserInfo> result = new ResultInfo<UserInfo>();
		
		// 校验
		if (StringUtils.isBlank(loginName) || StringUtils.isBlank(password)) {
			result.setCode(ResultCode.PARAMERROR.value);
			result.setMessage("参数为空");
			return result;
		}
		
		// 处理
		ResultInfo<Person> resultPerson = personService.login(loginName, password);
		result.setCode(resultPerson.getCode());
		result.setMessage(resultPerson.getMessage());
		if (result.getCode() == ResultCode.SUCCESS.value) {
			result.setInfo(convertToUserInfo(resultPerson.getInfo()));
		}
		
		return result;
	}
	
	@RequestMapping(value="/mappingUser.action")
	@ResponseBody
	public ResultInfo<UserInfo> mappingUser(String identity, String identityType) {
		ResultInfo<UserInfo> result = new ResultInfo<UserInfo>();
		
		// 校验
		if (StringUtils.isBlank(identity) || StringUtils.isBlank(identityType)) {
			result.setCode(ResultCode.PARAMERROR.value);
			result.setMessage("参数为空");
			return result;
		}

		IdentityType aIdentityType = IdentityType.parse(identityType);
		//if (!((IdentityType.email == aIdentityType) || (IdentityType.mobile == aIdentityType))) {
		if (IdentityType.email != aIdentityType) {
			result.setCode(ResultCode.PARAMERROR.value);
			result.setMessage("identityType参数目前仅支持email");
			return result;
		}
		
		// 处理
		Person person = this.personService.getByIdentity(identity, aIdentityType);
		if ((person == null) || (person.getValidState() != ValidState.Normal)) {
			result.setCode(ResultCode.FAIL.value);
			result.setMessage("身份不存在或无效");
			return result;
		}
		
		result.setCode(ResultCode.SUCCESS.value);
		result.setInfo(convertToUserInfo(person));
		return result;
	}	
	
	@RequestMapping(value="/modifyPassword.action")
	@ResponseBody	
	public Result modifyPassword(int userId, String oldPassword, String newPassword) {
		Result result = new Result();
		
		// 校验
		if (StringUtils.isBlank(oldPassword) || StringUtils.isBlank(newPassword)) {
			result.setCode(ResultCode.PARAMERROR.value);
			result.setMessage("参数为空");
			return result;
		}		
				
		return this.personService.modifyPassword(userId, oldPassword, newPassword);
	}
	
	
	private UserInfo convertToUserInfo(Person person) {		
		UserInfo result = new UserInfo();
		result.setUserId(person.getPersonId());
		result.setUserName(person.getLoginName());
		result.setOrgId(person.getOrgId());
		
		return result;
	}

}
